package com.jcfk.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.controller.BaseController;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.domain.dto.RefUserOrganizationMappingDTO;
import com.jcfk.system.domain.dto.SysOrganizationDTO;
import com.jcfk.system.domain.po.RefUserOrganizationMapping;
import com.jcfk.system.service.RefUserOrganizationMappingService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色菜单管理
 *
 * @author awei
 */
@Api(tags = {"用户组织机构管理"})
@RestController
@RequestMapping("/refUserOrganizationMapping")
public class RefUserOrganizationMappingController extends BaseController {

    @Autowired
    private RefUserOrganizationMappingService refUserOrganizationMappingService;

    @PostMapping("getlist")
    public RetResult<?> getlist(@Valid @RequestBody RefUserOrganizationMappingDTO refUserOrganizationMapping) {
        QueryWrapper<RefUserOrganizationMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RefUserOrganizationMapping::getUserId, refUserOrganizationMapping.getUserId());
        List<String> ids = refUserOrganizationMappingService.list(queryWrapper).stream().map(RefUserOrganizationMapping::getOrganizationId).collect(Collectors.toList());
        return RetResult.ok(ids);
    }

    @PostMapping("save")
    @RequestLogHeader(modular = "用户组织机构", businessType = "添加")
    public RetResult<?> save(@Valid @RequestBody RefUserOrganizationMappingDTO refUserOrganizationMapping) {
        refUserOrganizationMappingService.insert(refUserOrganizationMapping);
        return RetResult.ok();
    }


    /**
     * 用户组织机构
     *
     * @author awei
     */
    @PostMapping("getUserOrgans/{id}")
    @RequestLogHeader(modular = "用户组织机构", businessType = "添加")
    public RetResult<List<SysOrganizationDTO>> getUserOrgans(@PathVariable("id") String id) {
        return RetResult.ok(refUserOrganizationMappingService.getUserOrganList(id));
    }


    /**
     * 用户组织机构工厂
     *
     * @author awei
     */
    @PostMapping("getUserOrgansForFactory/{id}")
    @RequestLogHeader(modular = "用户组织机构工厂类型", businessType = "添加")
    public RetResult<List<SysOrganizationDTO>> getUserOrgansForFactory(@PathVariable("id") String id) {
        return RetResult.ok(refUserOrganizationMappingService.getUserOrgansTypeOfFactory(id));
    }

    /**
     * 查询人员
     *
     * @return
     */
    @GetMapping("/getUserOrganization/{userId}")
    public RetResult<List<RefUserOrganizationMappingDTO>> getUserOrganization(@PathVariable("userId") String userId) {
        List<RefUserOrganizationMappingDTO> ret = refUserOrganizationMappingService.getUserOrganization(userId);
        return RetResult.ok(ret);
    }

}
